package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**

 * @author Lucie
 *class Lend_Book_controler
 *Class variables:msg,flag
 *Class methods:get_data,check_input,get_msg,set_msg
 */

public class Lend_Book_controler {
	private String msg; 
	private boolean flag;
	/**
	 * Constructor initializes the class variables
	 */
	public Lend_Book_controler ()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	*  @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
	/**
	 * 
	 * @param copy_number
	 * @param memmber_id
	 	* this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 * then we cell the method "getUserdetails" hat check if the member exist and if its not a
	 * manager or a librarian if one of does are true we return false else we call the method 
	 * "checkItemLend" that return a few result
	 *  if the return result is "null" item copy not exist therefore can't lend item
	 *  if the return result is  "onorder" item is on order of another member therefore can't lend item
	 * if the return result is "StatusWrong" the card member is not active an the member can't lend a book
	 * if the return result is "NotOnShelf" the book is not on shelf member can't lend the book
	 * if the return result is "Duplicate" the selected item is currently hold by this user
	 *  if the return result is "Ok" we call the method "add_memmber_lend" that update in the data base that the book 
	 *  is lend and update reader card history
	 *  @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
		public boolean get_data(String copy_number,String memmber_id)
		{
			 ArrayList<String> str_meember_status= new  ArrayList<String>();
			 ArrayList<String> str_result=new  ArrayList<String>();
			 ArrayList<String> str_check=new  ArrayList<String>();
			
			 
			 check_input( copy_number, memmber_id);
			 if (flag==false)
				 return false;
			 
				str_check=BusinessImplements.GetInstance().getServerProxy().getUserdetails(memmber_id);
			 	if(str_check.get(0)==null)
			 	{
			 		set_msg("\nuser not exist"); 
			 		return false;
			 	}
				if(!(str_check.get(3).equals("1")))
			 	{
			 		set_msg("\ncant lend an item \nthe user is not a memmber"); 
			 		return false;
			 	}
			
			 str_meember_status=BusinessImplements.GetInstance().getServerProxy().checkItemLend(copy_number, memmber_id);
			
			 if (str_meember_status.get(0).equals("null"))
			 {
				 set_msg("\nitem not exist "); 
				 return false;
			 }
			 
			 if (str_meember_status.get(0).equals("onorder"))
			 {
				 set_msg("\nthis item is on order "); 
				 return false;
			 }
			 
			 if (str_meember_status.get(0).equals("StatusWrong"))
			 {
				 set_msg("\nmemmber status is not active "); 
				 return false;
			 }
			 if (str_meember_status.get(0).equals("NotOnShelf"))
			 {
				 set_msg("\nthe selected item copy is currently hold by anthor user\nplease check if item return correctly"); 
				 return false;
			 }
			 
			 if (str_meember_status.get(0).equals("Duplicate"))
			 {
				 set_msg("\nthe selected item is currently hold by this user"); 
				 return false;
			 }
			 if (str_meember_status.get(0).equals("OK"))
			 	{
				 
				 str_result=BusinessImplements.GetInstance().getServerProxy().add_memmber_lend(copy_number, memmber_id);
			
				 if (str_result==null)
				 { set_msg("\nexeption"); 
				 return false;
					 
				 }
			 if (str_result.get(0).equals("false"))
				 {
					 set_msg("\nmember lend book faild"); 
					 return false;
				 }
			   }
			 else
			 {
				 
				 set_msg("\nexception occur.");
				 return false;
			 }
			
				return flag;
		}
		/**
		 * 
		 * @param copy_number
		 * @param memmber_id
		 * *This method chain messages into a string ,
	 * the purpose ultimately is to display on the screen all the failures
		 */
		public void check_input(String copy_number,String memmber_id)
		{
			//
			if(memmber_id.equals(""))
			{
				set_msg("\nmising memmber id ");
				flag=false;
			}
			//
			if(copy_number.equals(""))
			{
				set_msg("\nmissing copy number ");
				flag=false;
			}
		}
	

	

}
